﻿using CleanArchitecture.Core.Events;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Core.Interfaces.Cache;
using CleanArchitecture.Infrastructure.Configuration;
using CleanArchitecture.Infrastructure.Entities.AdminUsers;
using CleanArchitecture.Infrastructure.Entities.Business;
using CleanArchitecture.Infrastructure.Entities.SystemModules;
using CleanArchitecture.Infrastructure.Services.Security;

namespace CleanArchitecture.Web.DomainEventHandler
{
    public partial class CacheEventHandler 
        : IHandle<PasswordChangedEvent<AdminUserPassword>>,
        //SystemModule
        IHandle<EntityInsertedEvent<SystemModule>>,
        IHandle<EntityUpdatedEvent<SystemModule>>,
        IHandle<EntityDeletedEvent<SystemModule>>,
        IHandle<EntityInsertedEvent<AdminRoleBusColumnMapping>>,
        IHandle<EntityUpdatedEvent<AdminRoleBusColumnMapping>>,
        IHandle<EntityDeletedEvent<AdminRoleBusColumnMapping>>,
        IHandle<SystemModuleAssignedButtonsChangedEvent>
    {
        #region Fields

        private readonly IStaticCacheManager _cacheManager;

        #endregion

        #region Ctor

        public CacheEventHandler(IStaticCacheManager cacheManager)
        {
            _cacheManager = cacheManager;
        }

        #endregion

        #region Methods

        //password changed
        public void Handle(PasswordChangedEvent<AdminUserPassword> eventMessage)
        {
            _cacheManager.Remove(string.Format(DefaultCacheConfig.AdminUserPasswordLifetimeCacheKey, eventMessage.Entity.FkUserId));
        }

        public void Handle(EntityInsertedEvent<SystemModule> eventMessage)
        {
            _cacheManager.Remove(DefaultCacheConfig.SystemNaviMenu);
        }
        public void Handle(EntityUpdatedEvent<SystemModule> eventMessage)
        {
            _cacheManager.Remove(DefaultCacheConfig.SystemNaviMenu);
        }
        public void Handle(EntityDeletedEvent<SystemModule> eventMessage)
        {
            _cacheManager.Remove(DefaultCacheConfig.SystemNaviMenu);
        }
        public void Handle(EntityInsertedEvent<AdminRoleBusColumnMapping> eventMessage)
        {
            RemoveAdminRoleColumnMappingEntityCacheKey();
        }
        public void Handle(EntityUpdatedEvent<AdminRoleBusColumnMapping> eventMessage)
        {
            RemoveAdminRoleColumnMappingEntityCacheKey();
        }
        public void Handle(EntityDeletedEvent<AdminRoleBusColumnMapping> eventMessage)
        {
            RemoveAdminRoleColumnMappingEntityCacheKey();
        }
        public void Handle(SystemModuleAssignedButtonsChangedEvent eventMessage)
        {
            string cacheKeyPrefix = DefaultCacheConfig.SystemModuleAssignedButtonsCacheKey;
            cacheKeyPrefix = cacheKeyPrefix.Substring(0, cacheKeyPrefix.IndexOf("."));
            _cacheManager.RemoveByPrefix(cacheKeyPrefix);
            cacheKeyPrefix = DefaultCacheConfig.SystemModuleAuthorizedCacheKey;
            cacheKeyPrefix = cacheKeyPrefix.Substring(0, cacheKeyPrefix.IndexOf("."));
            _cacheManager.RemoveByPrefix(cacheKeyPrefix);
        }
        #endregion

        private void RemoveAdminRoleColumnMappingEntityCacheKey()
        {
            string cacheKeyPrefix = DefaultCacheConfig.ColumnAuthorizedCacheKey;
            cacheKeyPrefix = cacheKeyPrefix.Substring(0, cacheKeyPrefix.IndexOf("."));
            _cacheManager.RemoveByPrefix(cacheKeyPrefix);
            cacheKeyPrefix = DefaultCacheConfig.ColumnListCacheKeyByRoles;
            cacheKeyPrefix = cacheKeyPrefix.Substring(0, cacheKeyPrefix.LastIndexOf("."));
            _cacheManager.RemoveByPrefix(cacheKeyPrefix); 
        }
    }
}
